package com.pwc.conversionTool.rest;

import java.util.Arrays;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.biz.AccountBiz;
import com.pwc.conversionTool.entity.Account;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.*;

@Controller
public class AccountRest {

	private static final Logger logger = LoggerFactory
			.getLogger(AccountRest.class);
	
	private AccountBiz biz=new AccountBiz();

	@Autowired
	public ServletContext context;

	@RequestMapping(value = "/rest/account/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean login(@RequestBody Account account, HttpSession session) {
		
		boolean result=false;
		
		try {
			result=biz.Login(account);
			
			if (result) {
				saveAccount(session,account);
			}
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return result;
	}

	@RequestMapping(value = "/rest/account/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean register(@RequestBody Account account,HttpSession session) {
		boolean result=false;
		try {
			result= biz.register(account);
			
			if (result) {
				saveAccount(session,account);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		
		
		return result;
	}

}
